<?php
// +----------------------------------------------------------------------
// | Iecms [ Make the most exquisite CMS ]
// +----------------------------------------------------------------------
// | Copyright (c) 2009~2019 http://www.iemoo.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: iemoo <i@iemoo.com>
// +----------------------------------------------------------------------
// | DateTime: 2019/11/19 11:08
// +----------------------------------------------------------------------

namespace app\admin\controller;

use app\common\model\SystemItem as SystemItemModel;
use app\common\model\SystemGroup as SystemGroupModel;

use app\admin\validate\SystemItem as SystemItemValidate;
use app\admin\validate\SystemGroup as SystemGroupValidate;

use think\exception\ValidateException;

use think\facade\Request;
use think\facade\View;
use think\facade\Config;


class System extends Base
{
 
    /**
     * 首页
     * @return mixed
     */
    public function index()
    {
		return View::fetch('index');
    }


    /**
     * 配置项管理
     * @return mixed
     */
    public function item()
    {

		if (Request::isPost()) {

            $data	= Request::only(['value']);

			$itemdata = [];

			foreach($data['value'] as $key => $val){
				SystemItemModel::update(['value' => $val], ['id' => $key]);
			}

			$this->success('保存成功',url('admin/system/item'));

        }



		$group = SystemGroupModel::where('id','<>',1)->select();
		foreach($group as $key => $val){
			$group[$key]->item = $val->items()->select();
		}
		$group = system_item($group);

		View::assign('group',$group);
		return View::fetch('item');
    }


    /**
     * 配置项分组
     * @return mixed
     */
    public function group()
    {
		$group = SystemGroupModel::select();

		View::assign('group',$group);
		return View::fetch('group');
    }
    /**
     * 配置项分组 添加
     * @return mixed
     */
    public function groupAdd()
    {

		if (Request::isPost()) {

            $data	= Request::only(['name', 'description', 'sort', 'status']);

            try {
				validate(SystemGroupValidate::class)->check($data);
			} catch (ValidateException $e) {
				// 验证失败 输出错误信息
				$this->error($e->getError());
			}

			if (SystemGroupModel::create($data)) {
				$this->success('保存成功',url('admin/system/group'));
			} else {
				$this->error('保存失败');
			}

        }

		return View::fetch();
    }

    /**
     * 配置项分组 编辑
     * @return mixed
     */
    public function groupEdit($id)
    {

		if (Request::isPost()) {

            $data	= Request::only(['id','name', 'description', 'sort', 'status']);

            try {
				validate(SystemGroupValidate::class)->scene('edit')->check($data);
			} catch (ValidateException $e) {
				// 验证失败 输出错误信息
				$this->error($e->getError());
			}

			if (SystemGroupModel::update($data, ['id' => $id])) {
				$this->success('保存成功',url('admin/system/group'));
			} else {
				$this->error('保存失败');
			}

        }

		$group = SystemGroupModel::find($id);
		View::assign('group',$group);
		return View::fetch();
    }


 

    /**
     * 配置项字段管理
     * @return mixed
     */
    public function field($group_id)
    {
		$group = SystemGroupModel::find($group_id);
		$field_list = SystemItemModel::where('system_group_id',$group_id)->paginate(10);

		View::assign('group',$group);
		View::assign('field_list',$field_list);
		
		return View::fetch();
    }

    /**
     * 配置项字段 添加
     * @return mixed
     */
    public function fieldAdd($group_id)
    {

		if (Request::isPost()) {

            $data	= Request::only(['system_group_id', 'field', 'name', 'tips', 'type', 'setup', 'required', 'sort', 'status']);

            try {
				validate(SystemItemValidate::class)->check($data);
			} catch (ValidateException $e) {
				// 验证失败 输出错误信息
				$this->error($e->getError());
			}

			if (SystemItemModel::create($data)) {
				$this->success('保存成功',url('admin/system/field',['group_id'=>$group_id]));
			} else {
				$this->error('保存失败');
			}

        }

		View::assign('group_id',$group_id);

		$group_list = SystemGroupModel::column('name','id');
		View::assign('group_list',$group_list);
		

		$fild_type = SystemItemModel::getType();
		View::assign('fild_type',$fild_type);

		return View::fetch();
    }

    /**
     * 配置项字段 编辑
     * @return mixed
     */
    public function fieldEdit($id)
    {

		if (Request::isPost()) {

            $data	= Request::only(['id','system_group_id', 'field', 'name', 'tips', 'type', 'setup', 'required', 'sort', 'status']);

            try {
				validate(SystemItemValidate::class)->check($data);
			} catch (ValidateException $e) {
				// 验证失败 输出错误信息
				$this->error($e->getError());
			}

			if (SystemItemModel::update($data, ['id' => $id])) {
				$this->success('保存成功',url('admin/system/field',['group_id'=>$data['system_group_id']]));
			} else {
				$this->error('保存失败');
			}

        }
		
		$item_info = SystemItemModel::find($id);
		View::assign('item_info',$item_info);

		$group_list = SystemGroupModel::column('name','id');
		View::assign('group_list',$group_list);

		$fild_type = SystemItemModel::getType();
		View::assign('fild_type',$fild_type);

		return View::fetch();
    }




}